package com.expense.common.gen;

import cn.hutool.core.util.StrUtil;
import org.beetl.core.Template;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.core.kit.GenKit;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.MapperCodeGen;
import org.beetl.sql.ext.gen.SourceGen;

import java.io.IOException;

public class ControllerCodeGen extends MapperCodeGen {
    String pkg = null;
    private String mapperTemplate = "";

    public ControllerCodeGen(String pkg) {
        this.pkg = pkg;
    }
    public ControllerCodeGen() {
        this.mapperTemplate = (new GenConfig()).getTemplate("/gen/controller.btl");
    }

    @Override
    public String getMapperTemplate() {
        return mapperTemplate;
    }

    @Override
    public void setMapperTemplate(String mapperTemplate) {
        this.mapperTemplate = mapperTemplate;
    }

    @Override
    public void genCode(String entityPkg, String entityClass, TableDesc tableDesc, GenConfig config, boolean isDisplay) {
        if (this.pkg == null) {
            this.pkg = entityPkg;
        }
        Template template = SourceGen.getGt().getTemplate(this.mapperTemplate);
        template.binding("className", entityClass);
        template.binding("camelClassName", StrUtil.lowerFirst(entityClass));
        template.binding("package", this.pkg);
        String mapperHead = "import " + entityPkg + ".*;" + SourceGen.CR;
        template.binding("imports", mapperHead);
        String mapperCode = template.render();
        if (isDisplay) {
            System.out.println(mapperCode);
        } else {
            try {
                SourceGen.saveSourceFile(GenKit.getJavaSRCPath(), this.pkg, entityClass+"Ctrl", mapperCode);
            } catch (IOException var11) {
                throw new RuntimeException("mapper代码生成失败", var11);
            }
        }

    }

}
